﻿namespace PrismEx.Extensions
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    public class NavigationUriBuilder
    {
        private readonly Type type;
        private readonly Dictionary<string, string> parameters = new Dictionary<string,string>();

        public static NavigationUriBuilder ForType<T>()
        {
            return new NavigationUriBuilder(typeof(T));
        }

        public NavigationUriBuilder WithParameter(string name, string value)
        {
            parameters.Add(name, value);
            return this;
        }

        private NavigationUriBuilder(Type type)
        {
            this.type = type;
        }

        public Uri Build()
        {
            var queryString = string.Join("&", parameters.Select(o => o.Key + "=" + o.Value));

            return new Uri(type.FullName + "?" + queryString, UriKind.Relative);
        }

        public static implicit operator Uri(NavigationUriBuilder builder)
        {
            return builder.Build();
        }
    }
}